package androidgames.networkgames.screens.components;

import android.graphics.Paint.Style;
import androidgames.configs.UIConfig;
import androidgames.networkgames.screens.BaseScreen;

import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Pixmap;
import com.badlogic.androidgames.framework.Screen;

public class DrawableButton extends DrawableComponent {
	Graphics g;
	public String strButton;
	int iTX;
	int iTY;
	long touchDownTime = 0;
	long touchDuration = 0;
	long firstTouch = 0;
	long secondTouch = 0;
	boolean isTouched = false;
	public DrawableButton(Screen screen, Pixmap background, int iWidth, int iHeight, int iX, int iY, int iSX, int iSY, String StrButtonText, int iTX, int iTY) {
		super(screen, background, iWidth, iHeight, iX, iY, iSX, iSY);
		// TODO Auto-generated constructor stub
		strButton = StrButtonText;
		this.iTX = iTX;
		this.iTY = iTY;
		g = ((BaseScreen)screen).g;
		this.background = background;
	}
	
	public DrawableButton(Screen screen, int iBackgroundColor, int iWidth, int iHeight, int iX, int iY, int iSX, int iSY, String StrButtonText, int iTX, int iTY) {
		super(screen, iBackgroundColor, iWidth, iHeight, iX, iY, iSX, iSY);
		// TODO Auto-generated constructor stub
		strButton = StrButtonText;
		this.iTX = iTX;
		this.iTY = iTY;
		g = ((BaseScreen)screen).g;
	}

	@Override
	public boolean update(float deltatime, TouchEvent touchevent) {
		// TODO Auto-generated method stub
		if(isTouched(touchevent.x, touchevent.y))
		{
			if(touchevent.type == TouchEvent.TOUCH_DOWN){
				touchDownTime = System.currentTimeMillis();
	    	}
	    	
	    	if(touchevent.type == TouchEvent.TOUCH_UP) {
	    		if(isTouched == false)
	    		{
	    			firstTouch = System.currentTimeMillis();
	    			isTouched = true;
	    		}	
	    		else
	    		{
	    			secondTouch = System.currentTimeMillis();
	    			isTouched = false;
	    		}	
				touchDuration = System.currentTimeMillis() - touchDownTime;
				CEvent cevent = new CEvent();
				if(touchDuration < 1000)
				{
	    			if((secondTouch - firstTouch) > UIConfig.LIMIT_TIME_DOUBLE_TOUCH || isTouched == true)
	    			{
	    				//Short touch
	    				cevent.iEventType = CEvent.TOUCH;
	    			}else
	    			{
	    				//Double touch
	    				cevent.iEventType = CEvent.DOUBLE_TOUCH;
	    				secondTouch = 0;
	    			}
				}else
				{
					//Long touch
	    			cevent.iEventType = CEvent.LONG_TOUCH;
				}
				fireEvent(cevent);
	    	}
	    	if(touchevent.type == TouchEvent.TOUCH_DRAGGED) {
	    		
	    	}
	    	return true;
		}
		return false;
	}

	@Override
	public void present(float deltatime, Graphics g) {
		// TODO Auto-generated method stub
		if(background != null)
			g.drawPixmap(background, iX, iY, iSourceX, iSourceY, iWidth, iHeight);
		else
			g.drawRect(iX, iY, iWidth, iHeight, iBackgroundColor);
		g.drawText(strButton, iTX, iTY, 0xf8f4ebff, 30, Style.FILL);
	}

}
